package com.hmf.contrller;

import com.hmf.bean.*;
import com.hmf.service.HmfMenuService;
import com.hmf.service.HmfUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody  //将方法的返回值，以特定的格式(json格式的数据)写入到response的body区域，进而将数据返回给客户端。
//上面两个等于@RestController，是上面两个的组合注解
public class HmfMenuContrller {

    @Autowired
    HmfMenuService menuService;

    //显示菜单栏
    @RequestMapping(value = "/auth/menu/showMenuByRoleId", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<Control>> showMenuByRoleId(@RequestBody Map<String, Integer> request) {
        ResponseMsg<List<Control>> res = new ResponseMsg<>();
        if (request.containsKey("roleId")) {
            int roleId = request.get("roleId");
            try {
                List<Control> control = menuService.queryMenuByRole(roleId);

                if (control != null && !control.isEmpty()) {
                    res.success("查询成功");
                    res.setData(control);
                } else {
                    res.fail("查询失败");
                }

            } catch (Exception e) {
                res.fail("查询失败");
            }
        }
        return res;
    }

    // 显示个人骑行记录
    @RequestMapping(value = "/auth/rideRecord", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<RideRecord>> searchRideRecord(@RequestBody Map<String, String> request1) {
        ResponseMsg<List<RideRecord>> res = new ResponseMsg<>();
        if (request1.containsKey("username")) {
            String username = request1.get("username");
            try {
                List<RideRecord> rideRecord = menuService.searchRideRecord(username);
                if (rideRecord != null) {
                    res.success("查询成功");
                    res.setData(rideRecord);
                } else {
                    res.fail("查询失败");
                }

            } catch (Exception e) {
                res.fail("查询失败");
            }
        }
        System.out.println("骑行记录："+res);
        return res;
    }

    // 显示单车状态
    @RequestMapping(value = "/auth/rideStatus", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<Bicycle>> searchRideRecord() {
        ResponseMsg<List<Bicycle>> res = new ResponseMsg<>();
            try {
                List<Bicycle> bicycle = menuService.searchRideStatus();
                if (bicycle != null) {
                    res.success("查询成功");
                    res.setData(bicycle);
                } else {
                    res.fail("查询失败");
                }
            } catch (Exception e) {
                res.fail("查询失败");
            }
        System.out.println("单车状态："+res);
        return res;
    }

    //故障单车登记
    @RequestMapping("/auth/insertRideRepair")
    public ResponseMsg<Map> insertRideRepair(@RequestBody RideRepair rideRepair) {
        ResponseMsg<Map> res = new ResponseMsg<Map>();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("bicycleNumber", rideRepair.getBicycleNumber());
        map.put("location", rideRepair.getLocation());
        map.put("yourReason", rideRepair.getUserReason());
        System.out.println(map);
        RideRepair r = new RideRepair();
        r.setBicycleNumber(rideRepair.getBicycleNumber());
        r.setLocation(rideRepair.getLocation());
        r.setUserReason(rideRepair.getUserReason());
        try {
            int repair = menuService.insertRideRepair(rideRepair);
            if (repair == 0) {
                res.success("登记成功");
                map.put("state", "200");
            } else {
                map.put("state", "201");
                res.fail("登记失败");
            }
        } catch (Exception e) {
            res.fail("登记失败");
            // 添加日志记录
            e.printStackTrace();
        }
        res.setData(map);
        return res;
    }

    // 所有个人骑行记录
    @RequestMapping(value = "/auth/allRideRecord", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<RideRecord>> searchAllRideRecord() {
        ResponseMsg<List<RideRecord>> res = new ResponseMsg<>();
            try {
                List<RideRecord> rideRecord = menuService.searchAllRideRecord();
                if (rideRecord != null) {
                    res.success("查询成功");
                    res.setData(rideRecord);
                } else {
                    res.fail("查询失败");
                }

            } catch (Exception e) {
                res.fail("查询失败");
            }
        System.out.println("所有用户骑行记录："+res);
        return res;
    }

    // 故障单车处理
    @RequestMapping(value = "/auth/repairRecode", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseMsg<List<RideRepair>> rideRepair() {
        ResponseMsg<List<RideRepair>> res = new ResponseMsg<>();
        try {
            List<RideRepair> rideRepair = menuService.rideRepair();
            if (rideRepair != null) {
                res.success("查询成功");
                res.setData(rideRepair);
            } else {
                res.fail("查询失败");
            }

        } catch (Exception e) {
            res.fail("查询失败");
        }
        System.out.println("故障单车表："+res);
        return res;
    }

    //故障单车处理完成
    @RequestMapping(value = "/auth/updateRepair", method = {RequestMethod.GET, RequestMethod.POST})
    public Map<String, Object> updateRepair(@RequestBody Map<String, Integer> request) {
        Map<String, Object> res = new HashMap<>();

        if (request.containsKey("repairID")) {
            int repairID = request.get("repairID");
            try {
                int result = menuService.updateRepair(repairID);
                if (result == 1) {
                    res.put("message", "更新成功");
                } else {
                    res.put("message", "更新失败");
                }
            } catch (Exception e) {
                res.put("message", "更新失败，发生错误");
                e.printStackTrace(); // 记录日志或其他错误处理
            }
        } else {
            res.put("message", "请求缺少repairID参数");
        }
        return res;
    }
}
